package org.anupta.game.server.game.packet.account.character.chat;

import org.anupta.game.main.Main;
import org.anupta.game.server.game.client.GameClient;
import org.anupta.game.server.game.packet.GamePacket;
import org.anupta.game.utils.others.Utils;

public class ChatMessage extends GamePacket {
	private char chan = ' ';
	private String message = null;

	public ChatMessage(GameClient client, String message) {
		super(client, message);
		unpack();
		doPacket();
	}

	@Override
	public void unpack() {
		chan = packetIn.charAt(2);
		packetIn.toString().replace("<", "");
		packetIn.toString().replace(">", "");
		if(!client.getPlayer().isHasTalk()){
			message = Utils.removeCarriageReturn(Utils.removeCarriageReturn(packetIn.toString().split("\\|")[1]));
			//TODO: Fix CarrageReturn in first packet...
			client.getPlayer().setHasTalk(true);
		}else{
			message = packetIn.toString().split("\\|")[1];
			
		}
	}

	@Override
	public void pack() {
	}

	@Override
	public void doPacket() {
		if(!client.getPlayer().getChannels().contains(chan+"")){
			return;
		}
		switch(chan){
		case '*':
			for(GameClient con : Main.game.getConnected()){
				if(!con.getPlayer().getChannels().contains("*")){
					continue;
				}
				con.send(new StringBuilder("cMK|").append(client.getPlayer().getGuid()).append("|").append(client.getPlayer().getName())
						.append("|").append(message).toString());
			}
			Main.out.println(new StringBuilder("Chat message sended").toString());
			break;
		case '#':
			for(GameClient con : Main.game.getConnected()){
				if(!con.getPlayer().getChannels().contains("#")){
					continue;
				}
				con.send(new StringBuilder("cMK#|").append(client.getPlayer().getGuid()).append("|").append(client.getPlayer().getName())
						.append("|").append(message).toString());
			}
			Main.out.println(new StringBuilder("Chat message sended").toString());
			break;
		case '$':
			for(GameClient con : Main.game.getConnected()){
				if(!con.getPlayer().getChannels().contains("$")){
					continue;
				}
				con.send(new StringBuilder("cMK$|").append(client.getPlayer().getGuid()).append("|").append(client.getPlayer().getName())
						.append("|").append(message).toString());
			}
			Main.out.println(new StringBuilder("Chat message sended").toString());
			break;
		case ':':
			for(GameClient con : Main.game.getConnected()){
				if(!con.getPlayer().getChannels().contains(":")){
					continue;
				}
				con.send(new StringBuilder("cMK:|").append(client.getPlayer().getGuid()).append("|").append(client.getPlayer().getName())
						.append("|").append(message).toString());
			}
			Main.out.println(new StringBuilder("Chat message sended").toString());
			break;
		case '@':
			for(GameClient con : Main.game.getConnected()){
				if(!con.getPlayer().getChannels().contains("@")){
					continue;
				}
				con.send(new StringBuilder("cMK@|").append(client.getPlayer().getGuid()).append("|").append(client.getPlayer().getName())
						.append("|").append(message).toString());
			}
			Main.out.println(new StringBuilder("Chat message sended").toString());
			break;
		case '?':
			for(GameClient con : Main.game.getConnected()){
				if(!con.getPlayer().getChannels().contains("?")){
					continue;
				}
				con.send(new StringBuilder("cMK?|").append(client.getPlayer().getGuid()).append("|").append(client.getPlayer().getName())
						.append("|").append(message).toString());
			}
			Main.out.println(new StringBuilder("Chat message sended").toString());
			break;
		case '%':
			for(GameClient con : Main.game.getConnected()){
				if(!con.getPlayer().getChannels().contains("%")){
					continue;
				}
				con.send(new StringBuilder("cMK%|").append(client.getPlayer().getGuid()).append("|").append(client.getPlayer().getName())
						.append("|").append(message).toString());
			}
			Main.out.println(new StringBuilder("Chat message sended").toString());
			break;
		case '!':
			for(GameClient con : Main.game.getConnected()){
				if(!con.getPlayer().getChannels().contains("!")){
					continue;
				}
				con.send(new StringBuilder("cMK!|").append(client.getPlayer().getGuid()).append("|").append(client.getPlayer().getName())
						.append("|").append(message).toString());
			}
			Main.out.println(new StringBuilder("Chat message sended").toString());
			break;
		default:
			String toPM = packetIn.substring(2).split("\\|")[0];
			for(GameClient con : Main.game.getConnected()){
				if(!con.getPlayer().getName().equals(toPM)){
					continue;
				}
				con.send(new StringBuilder("cMKF|").append(client.getPlayer().getGuid()).append("|").append(client.getPlayer().getName())
						.append("|").append(message).toString());
				client.send(new StringBuilder("cMKT|").append(con.getPlayer().getGuid()).append("|").append(con.getPlayer().getName())
						.append("|").append(message).toString());
			}
			Main.out.println(new StringBuilder("Chat message sended").toString());
			break;
		}
	}
}
